using System.Collections;
using Anvil.Core;

namespace Anvil.Core.Commands
{
    /// <summary>
    /// Create list of initial letters for entry names.
    /// </summary>
    public class EntryInitial : BaseList
    {
        /// <summary>
        /// Document token representing match all entries.
        /// </summary>
        public const string ALL = "[*]";

        public const string INITIAL = "initial";

        public override bool RequestExecute(IRequestContext context)
        {
            PreProcess(context); // Likes, et cetera.

            const string ZERO = "0";

            string[] input = {
                                 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
                                 "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
                                 "U", "V", "W", "X", "Y", "Z"
                             };

            IList output = new ArrayList(26);

            foreach (string letter in input)
            {
                string initial = letter + WILDCARD;
                context[INITIAL] = initial;
                object result = Mapper.QueryForObject(QueryID, context);
                if (ZERO.Equals(result)) continue;
                output.Add(letter);
            }

            output.Add(ALL);
            context.Outcome = output;
            return CONTINUE;
        }
    }
}